﻿Imports System.IO
Imports Persistencia

Public Class frmConexao

#Region " Campos de Instância "

    Private SQLCnn As New System.Data.SqlClient.SqlConnection
    Private SQLCmd As New System.Data.SqlClient.SqlCommand
    Private SQLDa As New System.Data.SqlClient.SqlDataAdapter
    Private SQLTr As System.Data.SqlClient.SqlTransaction
    Private sMensagemValidacao As String = ""
#End Region

#Region "Métodos"

    Private Sub cmdTestarConexao_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdTestarConexao.Click
        Dim sStringConexao As StreamWriter
        Dim sLocalExecutaval As String = Persistencia.perGlobais.prvLocalExe
        Dim sArquivo As String = "\SQL.DC"
        Dim sConexao As String
        Dim objSeguranca As New Controle.ctrSeguranca
        Dim login As String
        Dim senha As String
        Dim tentativa As Integer = 0

        Try
lblTentar:
            tentativa = tentativa + 1
            Select Case tentativa
                Case 1
                    login = "dslogin"
                    senha = "dssenha"
                Case 2
                    login = "dsLogin"
                    senha = "dssenha"
                Case 3
                    login = "dsLogin"
                    senha = "dsSenha"
                Case 4
                    login = "dslogin"
                    senha = "dsSenha"
                Case Else
                    login = "dslogin"
                    senha = "dssenha"
            End Select
            If Me.validarFormulario Then
                sConexao = "User ID=" & Trim(login) & ";Password=" & Trim(senha) & ";Initial Catalog=" & Trim(Me.txtBancoDados.Text) & ";Data Source=" & Me.txtServidor.Text
                If Me.SQLCnn.State = ConnectionState.Closed Then
                    Me.SQLCnn.ConnectionString = sConexao
                    Me.SQLCmd.Connection = Me.SQLCnn
                    Me.SQLCmd.CommandType = CommandType.Text
                    Me.SQLCnn.Open()
                End If
                If (IO.File.Exists(sLocalExecutaval + sArquivo)) Then
                    Kill(sLocalExecutaval + sArquivo)
                End If

                sStringConexao = New IO.StreamWriter(sLocalExecutaval + sArquivo, IO.FileMode.Create)
                sStringConexao.WriteLine(objSeguranca.criptografar(sConexao))
                sStringConexao.Close()
                SQLCnn.Close()

                MsgBox("Conexão estabelecida com sucesso", MsgBoxStyle.Information, Me.Text)
                Me.cmdOK.Enabled = True
                cmdOK.Focus()
            Else
                MsgBox(Me.sMensagemValidacao, MsgBoxStyle.Exclamation, Me.Text)
            End If

        Catch ex As SqlClient.SqlException
            If (tentativa <= 4) Then
                GoTo lblTentar
            End If
            MsgBox("Ocorreu um erro na conexão ao banco de dados" & Environment.NewLine & Environment.NewLine & ex.Message, MsgBoxStyle.Critical, Me.Text)
            Me.cmdOK.Enabled = False
        End Try

    End Sub

    Private Sub cmdSair_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSair.Click
        Me.Close()
    End Sub

    Private Sub cmdOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOK.Click
        Me.DialogResult = Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub frmConexao_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.txtBancoDados.Text = "DSClientes"
    End Sub

    Private Function validarFormulario() As Boolean

        Dim bRetorno As Boolean = True

        If Me.txtServidor.Text = String.Empty Then
            sMensagemValidacao = "- Servidor obrigatório."
            bRetorno = False
        End If

        If Me.txtBancoDados.Text = String.Empty Then
            sMensagemValidacao &= Environment.NewLine & "- Banco de dados obrigatório."
            bRetorno = False
        End If

        Return bRetorno

    End Function
#End Region




    
   
    
    
End Class